﻿// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="JBy.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.ShareTest.Core
{
    #region

    using OpenQA.Selenium;

    #endregion

    public class JBy : By
    {
        #region Public Methods

        /// <summary>
        /// 	JQuery selector
        /// </summary>
        /// <param name = "selector">
        /// 	The selector.
        /// </param>
        /// <returns>
        /// 	The j query.
        /// </returns>
        public static JQueryBy JQuery(string selector)
        {
            return new JQueryBy("(\"" + selector + "\")");
        }

        #endregion

        /// <summary>
        /// 	Specialized "By" class for JQuery selector
        /// </summary>
        public class JQueryBy
        {
            #region Constructors and Destructors

            public JQueryBy(string selector)
            {
                this.Selector = selector;
            }

            #endregion

            #region Public Properties

            public string Selector { get; set; }

            #endregion

            #region Public Methods

            public JQueryBy Children(string selector = "")
            {
                return this.Function("children", selector);
            }

            public JQueryBy Closest(string selector = "")
            {
                return this.Function("closest", selector);
            }

            public JQueryBy Eq(int index)
            {
                return this.Function("eq", index.ToString());
            }

            public JQueryBy Find(string selector = "")
            {
                return this.Function("find", selector);
            }

            public JQueryBy First()
            {
                return this.Function("first");
            }

            public JQueryBy Has(string selector)
            {
                return this.Function("has", selector);
            }

            public JQueryBy Last()
            {
                return this.Function("last");
            }

            public JQueryBy Next(string selector = "")
            {
                return this.Function("next", selector);
            }

            public JQueryBy NextAll(string selector = "")
            {
                return this.Function("nextAll", selector);
            }

            public JQueryBy NextUntil(string selector = "", string filter = "")
            {
                return this.Function("nextUntil", selector, filter);
            }

            public JQueryBy Not(string selector)
            {
                return this.Function("not", selector);
            }

            public JQueryBy OffsetParent()
            {
                return this.Function("offsetParent");
            }

            public JQueryBy Parent(string selector = "")
            {
                return this.Function("parent", selector);
            }

            public JQueryBy Parents(string selector = "")
            {
                return this.Function("parents", selector);
            }

            public JQueryBy ParentsUntil(string selector = "", string filter = "")
            {
                return this.Function("parentsUntil", selector, filter);
            }

            public JQueryBy Prev(string selector = "")
            {
                return this.Function("prev", selector);
            }

            public JQueryBy PrevAll(string selector = "")
            {
                return this.Function("prevAll", selector);
            }

            public JQueryBy PrevUntil(string selector = "", string filter = "")
            {
                return this.Function("prevUntil", selector, filter);
            }

            public JQueryBy Siblings(string selector = "")
            {
                return this.Function("siblings", selector);
            }

            #endregion

            #region Methods

            private JQueryBy Function(string func, string selector = "", string additionalArg = "")
            {
                // Add quotes to selector
                if (selector != string.Empty)
                {
                    selector = "\"" + selector + "\"";
                }

                // Add additional paramater
                if (additionalArg != string.Empty)
                {
                    selector += ",\"" + additionalArg + "\"";
                }

                // Add either: .func() or .func("selector") to original selector
                return new JQueryBy(this.Selector + "." + func + "(" + selector + ")");
            }

            #endregion
        }
    }
}